package com.itheima.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.pojo.*;
import com.itheima.queryParams.NursingPlanDto;
import com.itheima.queryParams.NursingPlanVo;
import com.itheima.service.NursingProjectPlanService;
import com.itheima.service.NursingPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/plan")
public class NursingPlanController {
    @Autowired
    private NursingPlanService nursingPlanService;
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    //条件分页查询
    @GetMapping
    public Result findAll(NursingPlanDto dto){
        PageResult pageResult = nursingPlanService.findAll(dto);
        return Result.success(pageResult);
    }

    //新增
    @Transactional
    @PostMapping
    public Result save(@RequestBody NursingPlanVo nursingPlanVo){
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(nursingPlanVo,nursingPlan);
        nursingPlanService.save(nursingPlan);

        List<NursingProjectPlan> list = nursingPlanVo.getNursingProject();
        if(list != null ){
            for (NursingProjectPlan nursingProjectPlan : list) {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlan.setCreateTime(LocalDateTime.now());
                nursingProjectPlan.setUpdateTime(LocalDateTime.now());
            }
            nursingProjectPlanService.saveBatch(list);
        }
        return Result.success();
    }

    //回显查询
    @GetMapping("/{id}")
    public Result queryPlanById(@PathVariable Integer id){
        NursingPlan nursingPlan = nursingPlanService.getById(id);
        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        nursingPlanVo.setId(id);
        nursingPlanVo.setPlanName(nursingPlan.getPlanName());
        nursingPlanVo.setStatus(nursingPlan.getStatus());
        nursingPlanVo.setSortNo(nursingPlan.getSortNo());
        nursingPlanVo.setNursingProject(list);
        return Result.success(nursingPlanVo);
    }

    //修改
    @Transactional
    @PutMapping
    public Result updateById(@RequestBody NursingPlanVo nursingPlanVo){
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(nursingPlanVo.getId());
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setPlanName(nursingPlanVo.getPlanName());
        nursingPlan.setStatus(nursingPlanVo.getStatus());
        nursingPlan.setSortNo(nursingPlanVo.getSortNo());
        nursingPlanService.updateById(nursingPlan);

        //先删除中间表的所有数据
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,nursingPlan.getId()));
        List<NursingProjectPlan> list = nursingPlanVo.getNursingProject();
        //然后在批量添加数据
        if (!ObjectUtils.isEmpty(list)){
            list.forEach(item->item.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.saveBatch(list);
        }
        return Result.success();
    }
}
